package com.eqdd.yiqidian.rongyun.message.link;

import android.os.Parcel;
import android.os.Parcelable;
import android.util.Log;

import com.eqdd.yiqidian.rongyun.message.mingpian.Content;
import com.eqdd.yiqidian.share.LinkShareBuilder;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.UnsupportedEncodingException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import io.rong.common.ParcelUtils;
import io.rong.common.RLog;
import io.rong.imlib.MessageTag;
import io.rong.imlib.model.MessageContent;
import io.rong.imlib.model.UserInfo;

/**
 * Created by lvzhihao on 17-4-30.
 */
@MessageTag(value = "EQD:lk", flag = MessageTag.ISCOUNTED | MessageTag.ISPERSISTED)
public class LinkMessage extends MessageContent implements Parcelable {

    private LinkContent content;


    public LinkMessage(LinkContent content) {
        this.content = content;
    }

    public static final Creator CREATOR = new Creator() {
        public LinkMessage createFromParcel(Parcel source) {
            return new LinkMessage(source);
        }

        public LinkMessage[] newArray(int size) {
            return new LinkMessage[size];
        }
    };


    public static LinkMessage obtain(LinkContent content) {
        return new LinkMessage(content);
    }

    private LinkMessage(Parcel in) {
        this.content = ParcelUtils.readFromParcel(in, LinkContent.class);
        this.setUserInfo(ParcelUtils.readFromParcel(in, UserInfo.class));
    }

    public LinkMessage() {
    }

    public LinkMessage(byte[] data) {
        String jsonStr = new String(data);

        try {
            JSONObject e = new JSONObject(jsonStr);
            if (e.has("user")) {
                this.setUserInfo(this.parseJsonToUserInfo(e.getJSONObject("user")));
            }
            if (e.has("content")) {
                this.content = parseJsonContent(e.getJSONObject("content"));

            }
        } catch (JSONException var4) {
            Log.e("JSONException", var4.getMessage());
        }

    }

    public LinkContent parseJsonContent(JSONObject jsonObj) {
        String title = jsonObj.optString("title");
        String content = jsonObj.optString("content");
        String imgUrl = jsonObj.optString("imgUrl");
        String url = jsonObj.optString("url");
        String source = jsonObj.optString("source");
        String articleId = jsonObj.optString("articleId");
        return new LinkContent(title, content, url, imgUrl, source, articleId);
    }

    public JSONObject getJSONContent() {
        if (this.getContent() != null) {
            JSONObject jsonObject = new JSONObject();
            try {
                jsonObject.put("title", this.getContent().getTitle());
                jsonObject.put("content", this.getContent().getContent());
                jsonObject.put("imgUrl", this.getContent().getImgUrl());
                jsonObject.put("url", this.getContent().getUrl());
                jsonObject.put("source", this.getContent().getSource());
                jsonObject.put("articleId", this.getContent().getArticleId());
            } catch (JSONException var3) {
                RLog.e("LinkMessageContent", "JSONException " + var3.getMessage());
            }

            return jsonObject;
        } else {
            return null;
        }
    }

    public byte[] encode() {
        JSONObject jsonObj = new JSONObject();
        try {
            if (this.getJSONUserInfo() != null) {
                jsonObj.putOpt("user", this.getJSONUserInfo());
            }
            if (this.getContent() != null) {
                jsonObj.putOpt("content", this.getJSONContent());
            }
        } catch (JSONException var4) {
            Log.e("JSONException", var4.getMessage());
        }

        try {
            return jsonObj.toString().getBytes("UTF-8");
        } catch (UnsupportedEncodingException var3) {
            var3.printStackTrace();
            return new byte[0];
        }
    }

    public int describeContents() {
        return 0;
    }

    public void writeToParcel(Parcel dest, int flags) {
        ParcelUtils.writeToParcel(dest, this.content);
        ParcelUtils.writeToParcel(dest, this.getUserInfo());
    }

    private String getExpression(String content) {
        Pattern pattern = Pattern.compile("\\[/u([0-9A-Fa-f]+)\\]");
        Matcher matcher = pattern.matcher(content);
        StringBuffer sb = new StringBuffer();

        while (matcher.find()) {
            matcher.appendReplacement(sb, this.toExpressionChar(matcher.group(1)));
        }

        matcher.appendTail(sb);
        Log.d("getExpression--", sb.toString());
        return sb.toString();
    }

    private String toExpressionChar(String expChar) {
        int inthex = Integer.parseInt(expChar, 16);
        return String.valueOf(Character.toChars(inthex));
    }

    public LinkContent getContent() {
        return content;
    }

    public void setContent(LinkContent content) {
        this.content = content;
    }
}
